﻿/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2012 Junhua
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of 
 * this software and associated documentation files (the "Software"), to deal in the 
 * Software without restriction, including without limitation the rights to use, copy, 
 * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, subject to the 
 * following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

namespace Reading.Controls
{
    using System;
    using System.Windows;
    using System.Windows.Media.Animation;

    public class Storyboards
    {
        const double Milliseconds = 400.0;

        Storyboard[] storyboards;

        static double width;

        static Storyboards()
        {
            width = Application.Current.Host.Content.ActualWidth + 20.0;
        }

        private Storyboards(Storyboard[] storyboards)
        {
            this.storyboards = storyboards;
        }

        public static Storyboards CreateLeftToRight(DependencyObject left, DependencyObject right)
        {
            Storyboard sb0 = new Storyboard();
            DoubleAnimation da0 = new DoubleAnimation();
            Storyboard sb1 = new Storyboard();
            DoubleAnimation da1 = new DoubleAnimation();

            QuadraticEase ease = new QuadraticEase();
            ease.EasingMode = EasingMode.EaseInOut;

            sb0.Children.Add(da0);
            da0.From = -width;
            da0.To = 0;
            da0.Duration = new Duration(TimeSpan.FromMilliseconds(Milliseconds));
            da0.EasingFunction = ease;
            Storyboard.SetTargetProperty(da0, new PropertyPath("(Canvas.Left)"));

            sb1.Children.Add(da1);
            da1.From = 0;
            da1.To = width;
            da1.Duration = new Duration(TimeSpan.FromMilliseconds(Milliseconds));
            da1.EasingFunction = ease;
            Storyboard.SetTargetProperty(da1, new PropertyPath("(Canvas.Left)"));

            Storyboard.SetTarget(da0, left);
            Storyboard.SetTarget(da1, right);

            return new Storyboards(new[] { sb0, sb1 });
        }

        public static Storyboards CreateRightToLeft(DependencyObject right, DependencyObject left)
        {
            Storyboard sb0 = new Storyboard();
            DoubleAnimation da0 = new DoubleAnimation();
            Storyboard sb1 = new Storyboard();
            DoubleAnimation da1 = new DoubleAnimation();

            QuadraticEase ease = new QuadraticEase();
            ease.EasingMode = EasingMode.EaseInOut;

            sb0.Children.Add(da0);
            da0.From = width;
            da0.To = 0;
            da0.Duration = new Duration(TimeSpan.FromMilliseconds(Milliseconds));
            da0.EasingFunction = ease;
            Storyboard.SetTargetProperty(da0, new PropertyPath("(Canvas.Left)"));

            sb1.Children.Add(da1);
            da1.From = 0;
            da1.To = -width;
            da1.Duration = new Duration(TimeSpan.FromMilliseconds(Milliseconds));
            da1.EasingFunction = ease;
            Storyboard.SetTargetProperty(da1, new PropertyPath("(Canvas.Left)"));

            Storyboard.SetTarget(da0, right);
            Storyboard.SetTarget(da1, left);

            return new Storyboards(new[] { sb0, sb1 });
        }

        public void Begin()
        {
            for (int i = 0; i < storyboards.Length; i++)
            {
                if (i == storyboards.Length - 1)
                {
                    if (Completed != null)
                    {
                        storyboards[i].Completed += (s, e) =>
                            {
                                Completed(this, EventArgs.Empty);
                            };
                    }
                }

                storyboards[i].Begin();
            }
        }

        public event EventHandler Completed;
    }
}
